const app = getApp()

Page({

  //页面的初始数据
  data: {
    loading: true,
    active:'0',
    page:0,
    order:[]
  },
  
  //监听页面加载
  onLoad: function (options) {
    this.setData({
      active:app.util().isNull(options.active)?0:options.active
    })
  },
  //监听页面显示
  onShow: function (options) {
    this.getApi();
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
    this.setData({
      page: 0,
      loading: true,
      order: []
    });
    this.getApi();
  },

  //上拉加载
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    this.getApi();
  },

  //读取我的订单
  getApi: function() {
    if (this.data.loading) {
      var param = {
        signkey: app.util().getRandom(12),
        page: this.data.page + 1,
        active: this.data.active
      }
      app.api().Get('api/v4/allwin/infodopay/order',param,(result) => {
        if (result.code == 200) {
          var order = this.data.order;
          for (let i in result.data) {
            order.push(result.data[i]);
          }
          this.setData({
            order: order,
          });
          this.data.page = param.page
        }
        this.setData({
          loading: false,
        });
      })
    }
  },

  //订单预览
  onViews(event){
    wx.navigateTo({
      url:'viewOrder?id='+event.currentTarget.id
    })
  },

  //订单核销
  onVar(event){
    wx.navigateTo({
      url:'viewOrder?var=1&id='+event.currentTarget.id
    })
  },

  //状态更改
  finished(event) {
    var order = this.data.item;
    order[event.currentTarget.dataset.id].is_del = 1;
    this.setData({
      item:order
    })
  },

  //Tab状态
  onChange(event) {
    this.setData({
      active:event.detail.name,
      item:[],
    })
    this.getApi();
  }
})